package com.android.bookgarden.ui;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.bookgarden.MyApplication;
import com.android.bookgarden.R;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.base.BaseActivity;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.bean.UserInfo;
import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.persenter.BasePersenter;
import com.android.bookgarden.utli.JsonUitl;
import com.android.bookgarden.utli.SpUtils;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.LoadingDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Create by XLC
 * on 2018/12/12
 * 修改昵称
 **/
public class EditNicknameUI extends BaseActivity implements View.OnClickListener ,RealtConstract.View {
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.ritgh_text)
    TextView ritghText;
    @BindView(R.id.nickname)
    EditText nickname;
    private RealtConstract.Presenter presenter;
    @Override
    protected int getLayoutId() {
        return R.layout.edit_nickname_ui;
    }

    @Override
    protected void initView() {
        presenter = new BasePersenter(this,this);
        titleText.setText("修改昵称");
        ritghText.setText("保存");
        ritghText.setVisibility(View.VISIBLE);
        ritghText.setTextColor(Color.parseColor("#23b16e"));
        ritghText.setOnClickListener(this);
        close.setOnClickListener(this);
    }

    @Override
    public void onRequestPermissions(String str, boolean z) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                finish();
                break;
            case R.id.ritgh_text:
                if (nickname.getText().toString() != null && !"".equals(nickname.getText().toString())) {
                    LoadingDialog.showMassge(this,"修改中...");
                    Map<String,String> map = new HashMap<>();
                    map.put("nickname",nickname.getText().toString());
                    presenter.loadDataGetHeader(map,"", Apis.CHANGE_NICK,1001, Utlis.getHeadler(this));

                }else {
                    Toast.makeText(this, "请设置昵称", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


    @Override
    public void returnData(String data, int urlCode) {

        if (TextUtils.isEmpty(data))
        {
            return;
        }
        handler.obtainMessage(urlCode,data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {
        LoadingDialog.hideProgress();
        showToast("请求失败");
    }


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String str = (String) msg.obj;
            switch (msg.what){
                case 1001:
                    LoadingDialog.hideProgress();
                    if (TextUtils.isEmpty(str)){
                        return;
                    }

                    DataBean bean = Utlis.getDataBean(str);
                    showToast(bean.getMessage());
                    if (bean.getCode()==200){
                        MyApplication.getInstance().userInfo.setNickname(nickname.getText().toString());
                        finish();

                    }else{

                    }
                    break;



            }

        }
    };
}
